/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.edu.assigment.survey;

import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author thanhtq
 */
@ManagedBean
@SessionScoped
public class ManagedBeanStudent {

    List<Survey> listSurveys;
    Survey survey;
    boolean checked;
    String comment;
    /** Creates a new instance of ManagedBeanStudent */
    public ManagedBeanStudent() {
    }

    

    public String loadQuestion()
    {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if(request.getParameter("id")!=null)
        {
            String id = request.getParameter("id");
            setSurvey(new SurveyDao().readSurvey(id));
        }
        return "question";
    }

    public void loadData()
    {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        if(request.getParameter("id")!=null)
        {
            String id = request.getParameter("id");
            setSurvey(new SurveyDao().readSurvey(id));
        }
    }


     public String display() {
            for (Question quest : getSurvey().getQuestion()) {
                for (Answer ans : quest.getListAnswer()) {
                    System.out.println(ans.checked+":"+ans.getRate());
                    if (ans.isChecked()) {                        
                        int rate = Integer.parseInt(ans.getRate()) + 1;
                        System.out.println(rate+getSurvey().getIdSurvey()+ans.getId());
                        new SurveyDao().update(rate, getSurvey().getIdSurvey(), ans.getId());
                    }
                   // System.out.println(ans.getRate() + " : " + ans.isChecked() + " rate : " + ans.getRating() + " : " + quest.getAmount());
                }
        }
        //loadData();
        return "statistic";
    }

    public Survey getSurvey() {
        return survey;
    }

    public void setSurvey(Survey survey) {
        this.survey = survey;
    }

    

    public List<Survey> getListSurveys() {
        return new SurveyDao().read();
    }

    public void setListSurveys(List<Survey> listSurveys) {
        this.listSurveys = listSurveys;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    
}
